home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
DICREATE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-07-29
|
5KB
|
159 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: dicreate.c
// Title: Data File I/O Library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to create a data file.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <di.h>
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
#define LF "\r\n"
#define CTRLZ "\x1A"
static CHAR szHeaderFormat[] = // File header format text
"Data File v%d.%02d" LF
"%s" LF
"Created %2d:%02d:%02d %2d/%02d/%4d." LF
"%s" LF
CTRLZ;
//----------------------------------------------------------------------------
// Description: Create a data file.
// The file is closed again after it is successfully created.
// Parameters: pcsz File name.
// An extension of '.dat' is appended if no other
// extension is specified.
// pcszUser Internal name of physical file
// pcszId Application id
// usMaxDir Maximum directory entries
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioCreate(PCSZ pcsz, PCSZ pcszUser, PCSZ pcszId, USHORT usMaxDir)
{
static time_t last_time = 0;
DATAHDR hdr;
DATADIR dir;
CHAR szName[MAX_PATH];
HF hf;
USHORT usDir;
USHORT fs = FL_TRUNCATE|FL_CREATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
time_t now = time(NULL);
struct tm *tm = localtime(&now);
if (pcszId == NULL)
pcszId = "DATA";
//
// Try to make sure files are created with unique time stamps....
//
if (now <= last_time)
now = last_time + 1;
//
// Validate the size of the various data structures.
//
Assert(sizeof(DATAHDR) == DFH_SIZE);
Assert(sizeof(DATADIR) == DFD_SIZE);
Assert(strlen(pcszUser) <= sizeof(hdr.chName));
Assert(((usMaxDir * sizeof(DATADIR)) % sizeof(DATAHDR)) == 0);
Assert(pcsz && pcszUser);
Assert(strlen(pcszId) == sizeof(hdr.szAppId));
usMaxDir = usMaxDir ? usMaxDir: 16;
Assert((LONG)usMaxDir * (LONG)sizeof(DATADIR) <= (LONG)32 _K);
//
// Initialize header information
//
memset(&hdr, 0, sizeof(hdr));
sprintf(hdr.szCopyright, szHeaderFormat,
(int)((DFH_VERION >> 8) & 0x00FF),
(int)(DFH_VERION & 0x00FF),
CfgGet(CFG_COPYRIGHT, NULL),
tm->tm_hour, tm->tm_min, tm->tm_sec,
tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
pcszUser);
hdr.lId = DFH_ID;
hdr.usVersion = DFH_VERION;
hdr.usDirectoryUsed = 0;
hdr.usDirectoryEntries = usMaxDir;
hdr.timetCreated = now;
hdr.timetModified = now;
memcpy(hdr.szAppId, pcszId, sizeof(hdr.szAppId));
memcpy(hdr.chName, pcszUser, strlen(pcszUser));
hdr.crc = (ULONG)CrcCalc((PBYTE)&hdr, sizeof(hdr) - sizeof(ULONG));
//
// Create file, write header and write empty directory entries.
//
strcpy(szName, pcsz);
FnameAppendExt(szName, di.szDataExt, TRUE);
if (!FnameQualify(szName, di.szDataExt, di.pcszDataPath, 0))
return FALSE;
if (!FileOpen(&hf, szName, fs, NULL))
return FALSE;
if (!FileWrite(hf, (PBYTE)&hdr, sizeof(hdr), 0L))
{
FileClose(hf);
return FALSE;
}
memset(&dir, 0, sizeof(dir));
dir.crc = (ULONG)CrcCalc((PBYTE)&dir, sizeof(dir) - sizeof(ULONG));
for (usDir = 0; usDir < usMaxDir; ++usDir)
{
if (!FileWrite(hf, (PBYTE)&dir, sizeof(dir), -1L))
{
FileClose(hf);
return FALSE;
}
}
return FileClose(hf);
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------